package com.caifeng.weatheractivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

/**
 * 解析JSON文件 示例
 */

public class JsonActivity extends AppCompatActivity implements View.OnClickListener {
    TextView weather, wind, temperature, pm, name;
    Button cq, sh, bj;
    ImageView icon;
    List<WeatherInfo> infoList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather);
        infoList=JsonPaper.getInstance().getInfosFromJson(JsonActivity.this);
        init();
        getCityData("重庆");


    }

    private void init() {
        cq = findViewById(R.id.cq);
        icon = findViewById(R.id.weather_icon);
        bj = findViewById(R.id.bj);
        sh = findViewById(R.id.sh);
        name = findViewById(R.id.city);
        weather = findViewById(R.id.weather_text);
        wind = findViewById(R.id.wind);
        pm = findViewById(R.id.pm);
        temperature = findViewById(R.id.temperature);

        cq.setOnClickListener(this);
        bj.setOnClickListener(this);
        sh.setOnClickListener(this);
    }

    private void setData(WeatherInfo info) {
        if (info == null) return;
            name.setText(info.getCity());
            pm.setText(info.getPm());
            temperature.setText(info.getTemp());
            weather.setText(info.getWeather());
            wind.setText(info.getWind());
            if (("多云").equals(info.getWeather())) {
                icon.setImageResource(R.mipmap.duoyun);
            } else if (("阴天").equals(info.getWeather())) {
                icon.setImageResource(R.mipmap.yintian);
            } else if (("晴天").equals(info.getWeather())) {
                icon.setImageResource(R.mipmap.qingtian);
            }

    }

    private void getCityData(String s) {
        for (WeatherInfo info : infoList) {
            if (info.getCity().equals(s)) {
                Log.e("222",info.getCity());
                setData(info);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.cq:
                getCityData("重庆");
                break;
            case R.id.sh:

                getCityData("上海");
                break;
            case R.id.bj:

                getCityData("北京");
                break;
        }
    }
}
